package com.github.pig.admin.util;

import org.apache.commons.io.output.WriterOutputStream;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil {
    /**
     * 返回斜杠拼接的字符串
     * @param args
     * @return
     */
    public static String mergeStringWithSeparator(String...args){
        StringBuilder sb=new StringBuilder();
        for (String arg : args) {
            sb.append(arg);
            sb.append(File.separator);
        }

        return sb.substring(0, sb.length()-1).toString();
    }

    /**
     * 输出文件
     * @param inputStream
     * @param response
     */
    public static void outputFile(InputStream inputStream, HttpServletResponse response){
        OutputStream stream= null;
        try {
            stream = response.getOutputStream();
            FileCopyUtils.copy(inputStream, stream);
            stream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(stream!=null){
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
